
cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

    },

    // update (dt) {},

    showActivityDialog: function (data, nagaCb) {
        cc.find("Canvas/DialogParent").removeAllChildren()
        cc.loader.loadRes("Activity/DailyCoin", cc.Prefab, function (err, prefab) {
            if (err) {
                cc.error(err)
                return
            }
            let node = cc.instantiate(prefab)
            cc.find("Canvas/DialogParent").addChild(node)
            node.getComponent("DailyCoin").initDailyCoin(data, nagaCb)
        }.bind(this))
    },

    removeActivityDialog: function () {
        cc.find("Canvas/DialogParent").removeAllChildren()
    },

    showWithdrawSuccess: function (withdrawCoin, sentence, cb) {
        cc.loader.loadRes("notice/NoticeWithdrawSucc", cc.Prefab, function (err, prefab) {
            if (err) {
                cc.error(err)
                return
            }
            let node = cc.instantiate(prefab)
            cc.find("Canvas").addChild(node)
            node.getComponent("NoticeWithdrawSucc").initNoticeWithdrawSucc(sentence, withdrawCoin, cb)
        }.bind(this))
    },

    showDialog: function (title, nagativeStr, passiveStr, contentStr, nagaCb, pasCb) {
        cc.find("Canvas/DialogParent").removeAllChildren()

        cc.loader.loadRes("notice/Dialog", cc.Prefab, function (err, prefab) {
            if (err) {
                cc.error(err)
                return
            }

            let newNode = cc.instantiate(prefab)
            cc.find("Canvas/DialogParent").addChild(newNode)
            // newNode.y -= 00
            this.dialogNode = newNode

            let dialogScript = newNode.getComponent("Dialog")
            dialogScript.labelTitle.string = title
            dialogScript.labelNaga.string = nagativeStr
            dialogScript.labelPass.string = passiveStr
            dialogScript.labelContent.string = contentStr
            dialogScript.setCbs(nagaCb, pasCb)

            // cc.loader.releaseRes("notice/Dialog")
        }.bind(this))
    },

    showDialogOneButton: function (title, nagativeStr, contentStr, nagaCb) {
        let dialogParent = cc.find("Canvas/DialogParent")
        if (dialogParent) {
            dialogParent.removeAllChildren()
        }

        cc.loader.loadRes("notice/DialogOneButton", cc.Prefab, function (err, prefab) {
            if (err) {
                cc.error(err)
                return
            }

            let newNode = cc.instantiate(prefab)
            dialogParent.addChild(newNode)
            // newNode.y -= 00
            this.dialogNode = newNode

            let dialogScript = newNode.getComponent("Dialog")
            dialogScript.labelTitle.string = title
            dialogScript.labelNaga.string = nagativeStr
            // dialogScript.labelPass.string = passiveStr
            dialogScript.labelContent.string = contentStr
            dialogScript.setCbs(nagaCb, null)

            // cc.loader.releaseRes("notice/Dialog")
        }.bind(this))
    },

    removeDialog: function () {
        cc.find("Canvas/DialogParent").removeAllChildren()
        return
    },

    showToast: function (string, duration) {
        cc.loader.loadRes("notice/ToastModel", cc.Prefab, function (err, prefab) {
            if (err) {
                cc.error(err)
                return
            }

            let newNode = cc.instantiate(prefab)
            cc.find("Canvas").addChild(newNode)
            newNode.y -= 100

            cc.find("LabelParent/Label", newNode).getComponent(cc.Label).string = string

            let array = []
            array.push(cc.fadeIn(0.2))
            array.push(cc.delayTime(duration || 0.4))
            array.push(cc.fadeOut(0.1))
            array.push(cc.removeSelf())
            newNode.runAction(cc.sequence(array))

            // cc.loader.releaseRes("notice/ToastModel")
        }.bind(this))
    },

    showScroll: function (data1, data2) {
        if(data1 == null && data2 == null) return;
        cc.loader.loadRes("notice/ScrollMsgModel", cc.Prefab, function (err, prefab) {
            if (err) {
                cc.error(err)
                return
            }
            if( !cc.find(`Canvas/noticeNode`)) {
                cc.log("没有noticeNode节点无法添加");
                return;
            }
            let oldNode = cc.find(`Canvas/noticeNode/ScrollMsgModel`)
            if (!cc.isValid(oldNode)) {
                let newNode = cc.instantiate(prefab)
                newNode.name = "ScrollMsgModel";
                cc.find(`Canvas/noticeNode`).addChild(newNode)
                newNode.getComponent("LobbyScrollMsgTs").setInfo(data1, data2);
            } else {
                oldNode.getComponent("LobbyScrollMsgTs").setInfo(data1, data2);
            }
        }.bind(this))
    },

    async showNetWaitting(callback) {
        cc.log("尝试显示网络等待画面")
        if (this.netWaittingNode && cc.isValid(this.netWaittingNode, true)) {
            if (callback) callback()
            cc.log("已经在网络等待画面，直接执行回调，返回")
            return
        }

        this.removeNetWaitting()
        cc.log("显示网络等待画面!")
        let prefab = await cc.gg.resUtil.loadRes("notice/NetWait")
        let newNode = cc.instantiate(prefab)
        newNode.name = "netwaitting"
        cc.find("Canvas").addChild(newNode)

        this.netWaittingNode = newNode
        if (callback) callback()
        return
        // cc.loader.releaseRes("notice/NetWait")
    },

    removeNetWaitting: function () {
        cc.log("清理网络等待画面!")
        if (this.netWaittingNode)
            this.netWaittingNode.destroy()

        let second = cc.find("Canvas/netwaitting")
        if (second)
            second.destroy()
        this.netWaittingNode = null
    },
});
